package com.atguigu.springboot.service.impl;

import com.atguigu.springboot.bean.DeptTreeVo;
import com.atguigu.springboot.bean.SysDept;
import com.atguigu.springboot.mapper.SysDeptDAO;
import com.atguigu.springboot.service.ISysDeptService;
import com.atguigu.springboot.utils.PreUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysDeptServiceImpl implements ISysDeptService {


    @Autowired(required = false)
    private SysDeptDAO sysDeptDAO;

    @Override
    public List<DeptTreeVo> getDeptTree() {
        List<SysDept> depts = sysDeptDAO.selectAllDept();
        List<DeptTreeVo> collect = depts.stream().filter(sysDept -> sysDept.getParentId() == 0 || sysDept.getParentId() == null)
                .map(sysDept -> {
                    DeptTreeVo deptTreeVo = new DeptTreeVo();
                    deptTreeVo.setId(sysDept.getDeptId());
                    deptTreeVo.setLabel(sysDept.getName());
                    return deptTreeVo;
                }).collect(Collectors.toList());
        PreUtil.findChildren1(collect, depts);
        return collect;
    }

    @Override
    public List <DeptTreeVo> getTree() {
        List<SysDept> depts = sysDeptDAO.selectAllDept();
        List<DeptTreeVo> treeList = new ArrayList<>();
        for (SysDept dept : depts) {
            if (0==dept.getParentId()){
                DeptTreeVo deptTreeVo = new DeptTreeVo();
                deptTreeVo.setId(dept.getDeptId());
                deptTreeVo.setLabel(dept.getName());
                deptTreeVo.setChildren(getChild(dept.getDeptId(),depts));
                treeList.add(deptTreeVo);
            }
        }
        return treeList;
    }


    /**
     * lamda表达式版
     */
    @Override
    public List <DeptTreeVo> getTreeEX() {
        List<SysDept> depts = sysDeptDAO.selectAllDept();
        return depts.stream().filter(dept->{return dept.getParentId().equals(0);}).map(dept ->{
            DeptTreeVo deptTreeVo = new DeptTreeVo();
            deptTreeVo.setId(dept.getDeptId());
            deptTreeVo.setLabel(dept.getName());
            deptTreeVo.setChildren(getChildEX(dept.getDeptId(),depts));
            return deptTreeVo;
        }).collect(Collectors.toList());
    }


    /**
     * 获取树节点
     * @param deptId
     * @param depts
     * @return
     */
    private List<DeptTreeVo> getChild(Integer deptId,List<SysDept> depts){
        List<DeptTreeVo> treeList = new ArrayList<>();
        for (SysDept dept : depts) {
            if (deptId.equals(dept.getParentId())){
                DeptTreeVo deptTreeVo = new DeptTreeVo();
                deptTreeVo.setId(dept.getDeptId());
                deptTreeVo.setLabel(dept.getName());
                deptTreeVo.setChildren(getChild(dept.getDeptId(),depts));
                treeList.add(deptTreeVo);
            }
        }
        return treeList;
    }


    /**
     * /**
     * lamda表达式版
     */
    private List<DeptTreeVo> getChildEX(Integer deptId,List<SysDept> depts){
        return depts.stream().filter(dept->{return deptId.equals(dept.getParentId());})
                .map(dept->{
                    DeptTreeVo deptTreeVo = new DeptTreeVo();
                    deptTreeVo.setId(dept.getDeptId());
                    deptTreeVo.setLabel(dept.getName());
                    deptTreeVo.setChildren(getChildEX(dept.getDeptId(),depts));
                    return deptTreeVo;
                }).collect(Collectors.toList());
    }
}
